package com.yls.except.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 消息dao
 *
 * @author xs
 * @version 1.0
 * @time 2016-3-1 下午3:55:18
 * @Description: {}
 */
@Repository(value = "systemDao")
public interface SystemDao {

    /**
     * 新增短信获取记录信息
     * @param map 短信获取记录信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_phonecode(phonecode_id,uid,phone,code,returncode,message,type,status,addtime,addip) " +
            "values " +
            "(#{phonecode_id},#{uid},#{phone},#{code},#{returncode},#{message},#{type},#{status},#{addtime},#{addip})")
    int insertPhoneCode(Map<String, Object> map);

    /**
     * 获取省列表信息
     * @return 省列表信息
     */
    @Select("select ProID pid, ProName name from yls_areaprovince")
    List<Map<String,Object>> selectProvinceList();

    /**
     * 获取市列表信息
     * @return 市列表信息
     */
    @Select("select CityID cid, CityName name from yls_areacity")
    List<Map<String,Object>> selectCityList();

    /**
     * 根据省id获取市列表信息
     * @param pid 省id
     * @return 市信息
     */
    @Select("select CityID cid, CityName name from yls_areacity where ProID=#{pid}")
    List<Map<String,Object>> selectCityListByPid(String pid);

    /**
     * 根据市id查询区信息
     * @param cid 市id
     * @return 区信息
     */
    @Select("select Id did,DisName name from yls_areadistrict where CityID=#{cid}")
    List<Map<String,Object>> selectDistrictListByCid(String cid);

    /**
     * 根据省id查询省信息
     * @param pid 省id
     * @return 省信息
     */
    @Select("select ProID pid, ProName name from yls_areaprovince where ProId=#{pid}")
    Map<String,Object> selectProvinceById(String pid);

    /**
     * 根据市id查询市信息
     * @param cid 市id
     * @return 市信息
     */
    @Select("select CityID cid, CityName name from yls_areacity where CityId=#{cid}")
    Map<String, Object> selectCityById(String cid);

    /**
     * 根据区id查询区信息
     * @param did 区id
     * @return 区信息
     */
    @Select("select Id did,DisName name from yls_areadistrict where Id=#{did}")
    Map<String, Object> selectDistrictById(String did);

    /**
     * 根据城市名称查询城市信息
     * @param citymap 城市名称
     * @return 城市信息
     */
    @Select("select CityID cid, CityName name from yls_areacity where CityName like ${cityname}")
    Map<String,Object> selectCityInfoByName(Map<String, Object> citymap);

    /**
     * 获取宝宝年龄信息
     * @return 宝宝年龄信息
     */
    @Select("select bady_age_id,minage,maxage from yls_badyage where isdisabled=1")
    List<Map<String,Object>> selectBadyAge();

    /**
     * 新增收货地址信息
     * @param addressinfo 收货地址信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_address " +
            "(address_id,uid,pid,cid,did,address,isdefault,phone,name,postcode,addtime,addip,isdisabled)" +
            "values" +
            "(#{address_id},#{uid},#{pid},#{cid},#{did},#{address},#{isdefault},#{phone},#{name},#{postcode},#{addtime},#{addip},#{isdisabled})")
    int insertAddress(Map<String, Object> addressinfo);

    /**
     * 获取我的收货地址
     * @param map
     * @return 收货地址新
     */
    @Select("select address_id,uid,pid,cid,did,address,isdefault,phone,name,postcode from yls_address where uid=#{uid} ${msg} and isdisabled=1 order by isdefault desc limit #{start},#{count}")
    List<Map<String, Object>> selectMyAddress(Map<String, Object> map);

    /**
     * 根据收货地址id查询收货地址信息
     * @param addressid 收货地址id
     * @return 收货地址信息
     */
    @Select("select address_id,uid,pid,cid,did,address,isdefault,phone,name,postcode,addtime,addip,isdisabled from yls_address where address_id=#{addressid}")
    Map<String,Object> selectAddressInfoById(String addressid);

    /**
     * 修改用户默认收货地址
     * @param uid 用户id
     * @return 修改状态（大于0成功，否则失败）
     */
    @Update("update yls_address set isdefault=0 where uid=#{uid}")
    int updateAddressIsDefault(String uid);

    /**
     * 修改用户收货地址信息
     * @param addressinfo 收货地址信息
     * @return 修改状态（大于0成功，否则失败）
     */
    @Update("update yls_address set " +
            "uid=#{uid},pid=#{pid},cid=#{cid},did=#{did},address=#{address},isdefault=#{isdefault},phone=#{phone},name=#{name},postcode=#{postcode},addtime=#{addtime},addip=#{addip},isdisabled=#{isdisabled} where address_id=#{address_id}")
    int updateAddressInfo(Map<String, Object> addressinfo);

    /**
     * 根据运费id查询运费信息
     * @param freight_id 运费id
     * @return 运费信息
     */
    @Select("select freight_id,freight_name,freightmoney,isfreight from yls_freight where freight_id=#{freight_id}")
    Map<String,Object> selectFreightInfoById(String freight_id);

//    /**
//     *  根据运费id和省id查询运费价格信息
//     * @param freightinfo 运费id和省id
//     * @return 运费价格信息
//     */
//    @Select("select freight_money_id,freight_id,pid,money,weight,weightprice from yls_freight_money where freight_id=#{freight_id} ${pid}")
//    Map<String,Object> selectFreightMoneyByFreightId(Map<String, Object> freightinfo);

    /**
     * 根据系统设置id查询系统设置信息
     * @param systemsetid 系统设置id
     * @return 系统设置信息
     */
    @Select("select system_set_id,content,setvalue from yls_system_set where system_set_id=#{systemsetid}")
    Map<String,Object> selectSystemSetInfoById(String systemsetid);

    /**
     * 根据用户id查询用户代理利率信息
     * @param uid 用户id
     * @return 用户代理利率信息
     */
    @Select("select r.rate from yls_proxy_rate r,yls_proxy_user u where u.uid=#{uid} and r.isdisabled=1 and u.isdisabled=1 and r.proxy_rate_id=u.proxy_rate_id")
    Map<String,Object> selectProxyUserRateByUid(String uid);

    /**
     * 新增推荐用户提成信息
     * @param invitemoney 推荐用户提成信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_invite_money_log " +
            "(invite_money_id,indent_num,indent_uid,uid,indent_money,percentage,addtime,addip,isdisabled) " +
            "values " +
            "(#{invite_money_id},#{indent_num},#{indent_uid},#{uid},#{indent_money},#{percentage},#{addtime},#{addip},#{isdisabled})")
    int insertInviteMoney(Map<String, Object> invitemoney);

    /**
     *  根据运费id查询运费价格信息
     * @param freight_id 运费id
     * @return 运费价格信息
     */
    @Select("select freight_money_id,freight_id,pid,money,weight,weightprice from yls_freight_money where freight_id=#{freight_id}")
    List<Map<String, Object>> selectFreightMoneyByFreightId(String freight_id);
}
